﻿#ifndef AAHEAD_H
#define AAHEAD_H

#include "AxisModule/singleaxis.h"
#include "AxisModule/xyzmodule.h"
#include "CompositeElement/sccylinder.h"
#include "CompositeElement/scvacuum.h"
#include "CompositeElement/twoaxisplanner.h"
#include "GTMotion/gtdispenser.h"
#include "Tray/trayconfig.h"
#include "TwoDimCali/twodimensioncalibration.h"
#include "VisionManager/visionmanager.h"
#include "Workers/AACore/aacore.h"
#include "aaheadconfig.h"
#include "namedefinition.h"
#include "systemCore/worker.h"
#include "uphhelper.h"

class AaHead : public Worker
{
    Q_OBJECT
public:
    AaHead(AACore *aaCore);
    ~AaHead() override;
    AaHeadConfig *getAAHeadConfig() const
    {
        return aaHeadConfig;
    }
    // Worker interface
    SCDO * getUVIO() { return this->uVOut; }

protected:
    virtual void initEventHandle() override;
    virtual void startEventHandle(const WorkerEvent &event) override;
    virtual void stopEventHandle() override {}
    virtual void allWorkerStopped() override;
    virtual void purgeEventHandle() override;
    virtual void resetEventHandle() override;
    virtual void homeWorker(bool moveToReadyPosAfterHome) override;

    //手动调试
public slots:
    void calibrateShotterToDownLookCameraOffset();
    void shotGlueDot();

    QString getDispensePathResultImageName();
    void saveImageForEditDispPath();
    void readDispensePath();

    void moveToUpDownLookPrPos();
    void excuteUpDownLookClibrate();
    void calibrateDownLookToUpLookCameraOffset();

    void pickLensManual();
    void placeRejectLensManual();
    void testCmd();

    void prToBond();

    void CalGripperPos(int pos);

    void shotGlueLine(double lineLen);
    void shotRectangle(double width, double height);
    void shotGlue(bool check);

    //自动运行
public slots:
    void startAuto();
    void sendResultToPA(QString type);
    void AAHeadRun(QString type);
    void doSensorPr();
    void grabLens();
    void doLensPr();
    void roughAA();
    void findBestPos();
    void aaFinish();
    void moveToShotGlue(bool doGlueCheck);
    void moveBackToBestPos();
    void assemblyProduct();
    void doUV();

private:
    QString dispensePathDir() const
    {
        return QString("%1/dispensePath/").arg(getWorkerConfigDir());
    }
    void moveToMushroomPosWithOffset();
    void executeGlue(QVector<PathEndPoint> &dispensePath);
    void lUTRejectVacuumGet();
    QVector<PathEndPoint> mapDispensePath(PrOffset offset, double shotGlueHeight);
    void placeNgLensToLUT();
    void moveToStanbyPos();
    bool glueCheck(QImage &imageBefore, QImage &imageAfter);

    void pickLens();

private:
    AACore *aaCore;

    XYZModule *sUTXYZ;
    XYZModule *aAThetaXYZ;

    AaHeadConfig *aaHeadConfig;
    ConfigFile *aaHeadConfigFile;

    VisionLocation *shotGlueDotPrLocation;
    VisionLocation *upLookCameraLocation;

    VisionLocation *lensPrLocation;
    VisionLocation *senserPrLocation;

    Calibration *glueParthCalibration;

    SCDO *shotGlueOut;
    SCCylinder *aaGripper;

    SCDO *uVOut;
    SCVacuum *sUTVacuum;
    SCVacuum *lUTVacuum;
    SCVacuum *LUTRejectVacuum;

    bool isAaHeadStopped = false;

    PrOffset uplookCalculateOffset = PrOffset(0, 0, 0);
    PrOffset downlookCalculateOffset = PrOffset(0, 0, 0);
    XYZModulePos bestPos;

    SCDispenser *dispenser;

    QVector<QPointF> mechDispPath;
};

class GrabLensErr : public SilicolError
{
public:
    GrabLensErr() : SilicolError(WorkerName::AaHead,
                                 QObject::tr("AA head grab lens failed!  \r\nOption explantion: \r\n"
                                             "Retry: grab again. \r\nAbort: System abort."))
    {
        appendErrorHandlingOption(Retry_);
        appendErrorHandlingOption(Abort_);
    }
protected:
    virtual void implemented() override {}
};

class GripperGetLensErr : public SilicolError
{
public:
    GripperGetLensErr() : SilicolError(WorkerName::AaHead,
                                 QObject::tr("AA head gripper in Clamp, would you like to extend it?  \r\nOption explantion: \r\n"
                                             "Extend: extend gripper. \r\nAbort: System abort."))
    {
        appendErrorHandlingOption(ErrorHandlingOption::Extend_);
        appendErrorHandlingOption(Abort_);
    }
protected:
    virtual void implemented() override {}
};

#endif    // AaHead_H
